package com.zw.a.basic.a14.demo06_tcp_exercise;

//案例: 服务端：接收到数据在控制台输出

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

//自定义的TCP协议的服务器端
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //1. 创建服务器端的Socket对象.
        ServerSocket server = new ServerSocket(10010);
        //2. 监听客户端连接.
        Socket accept = server.accept();

        //3. 获取输入流, 可以读取客户端写过来的数据.
        //InputStream is = accept.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));

        //4. 读取客户端写过来的数据, 并打印.
        String line;
        while((line = br.readLine()) != null) {
            System.out.println(line);
        }

        //6. 关闭Socket对象.
        accept.close();
    }
}
